package avicit.bdp.dms.tdm.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.dms.tdm.dao.BdpAssetMountDataServiceDao;
import avicit.bdp.dms.tdm.dto.BdpAssetDTO;
import avicit.bdp.dms.tdm.dto.BdpAssetMountDataServiceDTO;
import avicit.platform6.api.system.SysAutoCodeClient;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.PojoUtil;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.exception.DaoException;
import avicit.platform6.core.properties.PlatformConstant;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BdpAssetMountDataServiceService
        extends BaseService<BdpAssetMountDataServiceDao, BdpAssetMountDataServiceDTO> {

    @Autowired
    private BdpAssetService assetService;
    @Autowired
    private SysAutoCodeClient sysAutoCodeClient;
    /**
     * 通过主键查询单条记录
     *
     * @param id 主键id
     */
    @Transactional(readOnly = true)
    public BdpAssetMountDataServiceDTO queryById(String id) {
        try {
            BdpAssetMountDataServiceDTO dto = this.selectByPrimaryKey(id);
            // 记录日志
            if (dto != null) {
                SysLogUtil.log4Query(dto);
            }
            return dto;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    /**
     * 新增对象
     *
     * @param dto 保存对象
     * @return String
     */
    @Transactional
    public String save(BdpAssetMountDataServiceDTO dto) {
        try {
            if (StringUtils.isNoneBlank(dto.getApiAddress(), dto.getRequestMethod())) {
                BdpAssetMountDataServiceDTO param = new BdpAssetMountDataServiceDTO();
                param.setApiAddress(dto.getApiAddress());
                param.setRequestMethod(dto.getRequestMethod());
                if (this.selectCount(param) > 0) {
                    throw new BusinessException("该API地址和请求方式已存在");
                }
            }
            dto.setId(ComUtil.getId());
            PojoUtil.setSysProperties(dto, PlatformConstant.OpType.insert);
            this.insert(dto);
            if (StringUtils.isNotBlank(dto.getOpenServiceType())) {
                BdpAssetDTO assetDTO = assetService.selectByPrimaryKey(dto.getAssetId());
                assetDTO.setOpenServiceType(dto.getOpenServiceType());
                assetService.updateByPrimaryKey(assetDTO);
            }
            // 记录日志
            SysLogUtil.log4Insert(dto);
            return dto.getId();
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    /**
     * 修改对象部分字段
     *
     * @param dto 修改对象
     * @return int
     */
    @Transactional
    public int update(BdpAssetMountDataServiceDTO dto) {
        try {

            BdpAssetMountDataServiceDTO oldDTO = selectByPrimaryKey(dto.getId());
            if (oldDTO == null) {
                throw new DaoException("数据不存在");
            }

            if (StringUtils.isNoneBlank(dto.getApiAddress(), dto.getRequestMethod())
                    && !(dto.getApiAddress().equalsIgnoreCase(oldDTO.getApiAddress())
                    && dto.getRequestMethod().equalsIgnoreCase(oldDTO.getRequestMethod()))) {
                BdpAssetMountDataServiceDTO param = new BdpAssetMountDataServiceDTO();
                param.setApiAddress(dto.getApiAddress());
                param.setRequestMethod(dto.getRequestMethod());
                if (this.selectCount(param) > 0) {
                    throw new BusinessException("该API地址和请求方式已存在");
                }
            }

            int count = this.updateByPrimaryKey(getUpdateDto(dto));
            if (count == 0) {
                throw new DaoException("数据失效，请重新更新");
            }
            if (StringUtils.isNotBlank(dto.getOpenServiceType())) {
                BdpAssetDTO assetDTO = assetService.selectByPrimaryKey(dto.getAssetId());
                assetDTO.setOpenServiceType(dto.getOpenServiceType());
                assetService.updateByPrimaryKey(assetDTO);
            }
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    /**
     * 内部方法，获取修改的dto对象
     */
    private BdpAssetMountDataServiceDTO getUpdateDto(BdpAssetMountDataServiceDTO dto) {
        BdpAssetMountDataServiceDTO oldDTO = selectByPrimaryKey(dto.getId());
        if (oldDTO == null) {
            throw new DaoException("数据不存在");
        }
        // 记录日志
        SysLogUtil.log4Update(dto, oldDTO);
        PojoUtil.setSysProperties(dto, PlatformConstant.OpType.update);
        PojoUtil.copyProperties(oldDTO, dto, true);
        return oldDTO;
    }

    public BdpAssetMountDataServiceDTO queryByAssetId(String assetId) {
        //BdpAssetMountDataServiceDTO query = new BdpAssetMountDataServiceDTO();
        //query.setAssetId(assetId);
        return this.mapper.queryByAssetId(assetId);
    }

    public Boolean apiAddressIsExiet(String apiAddress) {
        Integer count = this.mapper.countApiAddress(apiAddress);
        if (count > 0) {
            return true;
        }
        return false;
    }

    public String getPlatformCode(String code) {
        return sysAutoCodeClient.autoGenerationCodeValue(null, code, code, false, null).get("autoCodeValue");
    }

    public BdpAssetMountDataServiceDTO getByRequestUri(String requestUri) {
        return this.mapper.getByRequestUri(requestUri);
    }
}
